<?php

namespace backend\modules\rbac\models;

use mdm\admin\models\Menu as MdmMenu;

/**
 * User model
 */
class Menu extends MdmMenu
{

    /**
     * @desc 给一个默认值
     */
    public function init() {
        parent::init();
        if ($this->isNewRecord) {
            $this->order = 0;
            $this->route = '';
        }
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'parent' => '父级',
            'parent_name' => '父级名称',
            'route' => '路由',
            'order' => '排序',
            'data' => '数据',
        ];
    }

    public function rules()
    {
        return [
            [['name'], 'required', 'on'=>'insert'],
            [['parent_name'], 'in',
                'range' => static::find()->select(['name'])->column(),
                'message' => 'Menu "{value}" not found.'],
            [['parent', 'route', 'data', 'order','name', 'id'], 'default'],
            [['parent'], 'filterParent', 'when' => function() {
                return !$this->isNewRecord;
            }],
            [['order'], 'integer'],
            [['route'], 'in',
                'range' => static::getSavedRoutes(),
                'message' => 'Route "{value}" not found.']
        ];
    }
}
